using System;
using System.Collections.Generic;
using System.Linq;
using Matriks;
using Matriks.Data.Symbol;
using Matriks.Engines;
using Matriks.Indicators;
using Matriks.Symbols;
using Matriks.Trader.Core;
using Matriks.Trader.Core.Fields;
using Matriks.Lean.Algotrader.AlgoBase;
using Matriks.Lean.Algotrader.Models;
using Matriks.Lean.Algotrader.Trading;
using Matriks.AI;
using Matriks.AI.AiParameters;
using Matriks.AI.Data;
using Matriks.Trader.Core.TraderModels;
namespace Matriks.Lean.Algotrader
{
public class Ott_Stock_HHVLLV_Hott_Lott_firsatciTrend_SHORT : MatriksAlgo
{
[SymbolParameter("EREGL")]
public string Symbol1;
[Parameter(SymbolPeriod.Min)]
public SymbolPeriod SymbolPeriod1;
[Parameter(20)]
public int MajorTrend_OttPeriod;
[Parameter(7)]
public decimal MajorTrend_OttOPT;
[Parameter(20)]
public int MinorTrend_TottPeriod;
[Parameter(0.6)]
public decimal MinorTrend_TottOPT;
[Parameter(0.0006)]
public decimal MinorTrend_TottTwinOttCoef;
[Parameter(200)]
public int MinorTrend_SottPeriodK;
[Parameter(200)]
public int MinorTrend_SottPeriodSlowK;
[Parameter(2)]
public int MinorTrend_SottOttPeriod;
[Parameter(0.4)]
public decimal MinorTrend_SottOttOPT;
[Parameter(46)]
public int MinorTrend_LLV;
[Parameter(2)]
public int MinorTrend_LOttPeriod;
[Parameter(0.6)]
public decimal MinorTrend_LOttOpt;
[Parameter(46)]
public int MinorTrend_REFLLV;
[Parameter(20)]
public int FirsatciTrend_OttPeriod;
[Parameter(3)]
public decimal FirsatciTrend_OttOpt;
[Parameter(30)]
public int MinorTrend2_TottPeriod;
[Parameter(0.6)]
public decimal MinorTrend2_TottOpt;
[Parameter(0.0006)]
public decimal MinorTrend2_TottTwinOttCoef;
[Parameter(250)]
public int MinorTrend2_SottPeriodK;
[Parameter(200)]
public int MinorTrend2_SottPeriodSlowK;
[Parameter(2)]
public int MinorTrend2_SottOttPeriod;
[Parameter(0.2)]
public decimal MinorTrend2_SottOttOpt;
[Parameter(34)]
public int MinorTrend2_LLV;
[Parameter(2)]
public int MinorTrend2_LOttPeriod;
[Parameter(0.6)]
public decimal MinorTrend2_LOttOpt;
[Parameter(34)]
public int MinorTrend2_REFLLV;
[Parameter(30)]
public int MinorTrend3_TottPeriod;
[Parameter(0.6)]
public decimal MinorTrend3_TottOpt;
[Parameter(0.0006)]
public decimal MinorTrend3_TottTwinOttCoef;
[Parameter(250)]
public int MinorTrend3_SottPeriodK;
[Parameter(200)]
public int MinorTrend3_SottPeriodSlowK;
[Parameter(2)]
public int MinorTrend3_SottOttPeriod;
[Parameter(0.3)]
public decimal MinorTrend3_SottOttOpt;
[Parameter(16)]
public int MinorTrend3_HHV;
[Parameter(2)]
public int MinorTrend3_HOttPeriod;
[Parameter(0.6)]
public decimal MinorTrend3_HOttOpt;
[Parameter(16)]
public int MinorTrend3_REFHHV;
[Parameter(30)]
public int MinorTrend4_TottPeriod;
[Parameter(0.6)]
public decimal MinorTrend4_TottOpt;
[Parameter(0.0008)]
public decimal MinorTrend4_TottTwinOttCoef;
[Parameter(251)]
public int MinorTrend4_SottPeriodK;
[Parameter(200)]
public int MinorTrend4_SottPeriodSlowK;
[Parameter(2)]
public int MinorTrend4_SottOttPeriod;
[Parameter(0.3)]
public decimal MinorTrend4_SottOttOpt;
[Parameter(22)]
public int MinorTrend4_HHV;
[Parameter(2)]
public int MinorTrend4_HOttPeriod;
[Parameter(0.6)]
public decimal MinorTrend4_HOttOpt;
[Parameter(22)]
public int MinorTrend4_REFHHV;
[Parameter(1)]
public decimal OrderQuantity;
OTT ott;
TOTT tott;
SOTT sott;
LowestLow lowestLow;
OTT ott2;
LowestLow lowestLow2;
OTT ott3;
TOTT tott2;
SOTT sott2;
LowestLow lowestLow3;
OTT ott4;
LowestLow lowestLow4;
TOTT tott3;
SOTT sott3;
HighestHigh highestHigh;
OTT ott5;
HighestHigh highestHigh2;
TOTT tott4;
SOTT sott4;
HighestHigh highestHigh3;
OTT ott6;
HighestHigh highestHigh4;
OTT ott7;
public enum ZamanKullanilsinMI
{
EVET,
HAYIR
}
[Parameter(ZamanKullanilsinMI.EVET)]
public ZamanKullanilsinMI Zaman;
public bool ZamanKontrol;
[Parameter("10:03:00")]
public string Baslangic;
[Parameter("17:58:00")]
public string Bitis;
public bool FX_ZamanindaMI(DateTime zaman)
{
var bas = TimeSpan.Parse(Baslangic);
var bit = TimeSpan.Parse(Bitis);
return (zaman.TimeOfDay >= bas && zaman.TimeOfDay <= bit);
}
[Parameter(3)]
public int Kaldirac;
public override void OnDataUpdate(BarDataEventArgs barData)
{
var ZamanKontrol = Zaman == ZamanKullanilsinMI.EVET ? FX_ZamanindaMI(barData.BarData.Dtime) == true? true:false: true;
var barData1 = GetBarData(Symbol1, SymbolPeriod1);
var ohlcData1 = GetSelectedValueFromBarData(barData1, OHLCType.Low);
var ohlcData2 = GetSelectedValueFromBarData(barData1, OHLCType.High);
if (ZamanKontrol && ott.Value[1][ott.CurrentIndex] < ott.Value[0][ott.CurrentIndex])
{
if (tott.Value[0][tott.CurrentIndex] < tott.Value[2][tott.CurrentIndex]
&& sott.Value[0][sott.CurrentIndex] < sott.Value[1][sott.CurrentIndex]
&& ott2.Value[0][ott2.CurrentIndex] > ohlcData1
&& lowestLow2.Value[0][lowestLow2.CurrentIndex - 1] > ohlcData1)
{
SendShortSaleMarketOrder(Symbol1, OrderQuantity);
Debug("Açığa satış yapıldı.");
}
}else if (ZamanKontrol && ott7.Value[1][ott7.CurrentIndex] < (ott7.Value[0][ott7.CurrentIndex] + ott7.Value[0][ott7.CurrentIndex -100] - ott.Value[0][ott.CurrentIndex -100] + ott7.Value[0][ott7.CurrentIndex - 100]) / 2
&& tott2.Value[0][tott2.CurrentIndex] < tott2.Value[2][tott2.CurrentIndex]
&& sott2.Value[0][sott2.CurrentIndex] < sott2.Value[1][sott2.CurrentIndex]
&& ott4.Value[0][ott4.CurrentIndex] > ohlcData1
&& lowestLow4.Value[0][lowestLow4.CurrentIndex - 1] > ohlcData1)
{
SendShortSaleMarketOrder(Symbol1, OrderQuantity);
Debug("Açığa satış yapıldı.");
}
if (ZamanKontrol && ott.Value[1][ott.CurrentIndex] < ott.Value[0][ott.CurrentIndex])
{
if (tott3.Value[0][tott3.CurrentIndex] > tott3.Value[1][tott3.CurrentIndex]
&& sott3.Value[0][sott3.CurrentIndex] > sott3.Value[1][sott3.CurrentIndex]
&& ott5.Value[0][ott5.CurrentIndex] < ohlcData2
&& highestHigh2.Value[0][highestHigh2.CurrentIndex - 1] < ohlcData2)
{
SendMarketCloseShortOrder(Symbol1, OrderQuantity);
Debug("Açık pozisyon kapatıldı.");
}
}else if (ZamanKontrol && tott4.Value[0][tott4.CurrentIndex] > tott4.Value[1][tott4.CurrentIndex]
&& sott4.Value[0][sott4.CurrentIndex] > sott4.Value[1][sott4.CurrentIndex]
&& ott6.Value[0][ott6.CurrentIndex] < ohlcData2
&& highestHigh4.Value[0][highestHigh4.CurrentIndex - 1] < ohlcData2)
{
SendMarketCloseShortOrder(Symbol1, OrderQuantity);
Debug("Açık pozisyon kapatıldı.");
}
}
public override void OnInit()
{
ott = OTTIndicator(Symbol1, SymbolPeriod1, OHLCType.Close, MajorTrend_OttPeriod, MajorTrend_OttOPT, MovMethod.VAR, true);
tott = TOTTIndicator(Symbol1, SymbolPeriod1, OHLCType.Close, MinorTrend_TottPeriod, MinorTrend_TottOPT, MinorTrend_TottTwinOttCoef, MovMethod.VAR);
sott = SOTTIndicator(Symbol1, SymbolPeriod1, OHLCType.Close, MinorTrend_SottPeriodK, MinorTrend_SottPeriodSlowK, MinorTrend_SottOttPeriod, MinorTrend_SottOttOPT, MovMethod.VAR, MovMethod.VAR);
lowestLow = LowestLowIndicator(Symbol1, SymbolPeriod1, MinorTrend_LLV / 2);
ott2 = OTTIndicator(lowestLow, MinorTrend_LOttPeriod, MinorTrend_LOttOpt, MovMethod.VAR, true);
lowestLow2 = LowestLowIndicator(Symbol1, SymbolPeriod1, MinorTrend_REFLLV);
tott2 = TOTTIndicator(Symbol1, SymbolPeriod1, OHLCType.Close, MinorTrend2_TottPeriod, MinorTrend2_TottOpt, MinorTrend2_TottTwinOttCoef, MovMethod.VAR);
sott2 = SOTTIndicator(Symbol1, SymbolPeriod1, OHLCType.Close, MinorTrend2_SottPeriodK, MinorTrend2_SottPeriodSlowK, MinorTrend2_SottOttPeriod, MinorTrend2_SottOttOpt, MovMethod.VAR, MovMethod.VAR);
lowestLow3 = LowestLowIndicator(Symbol1, SymbolPeriod1, MinorTrend2_LLV / 2);
ott4 = OTTIndicator(lowestLow3, MinorTrend2_LOttPeriod, MinorTrend2_LOttOpt, MovMethod.VAR, true);
lowestLow4 = LowestLowIndicator(Symbol1, SymbolPeriod1, MinorTrend2_REFLLV);
tott3 = TOTTIndicator(Symbol1, SymbolPeriod1, OHLCType.Close, MinorTrend3_TottPeriod, MinorTrend3_TottOpt, MinorTrend3_TottTwinOttCoef, MovMethod.VAR);
sott3 = SOTTIndicator(Symbol1, SymbolPeriod1, OHLCType.Close, MinorTrend3_SottPeriodK, MinorTrend3_SottPeriodSlowK, MinorTrend3_SottOttPeriod, MinorTrend3_SottOttOpt, MovMethod.VAR, MovMethod.VAR);
highestHigh = HighestHighIndicator(Symbol1, SymbolPeriod1, MinorTrend3_HHV / 2);
ott5 = OTTIndicator(highestHigh, MinorTrend3_HOttPeriod, MinorTrend3_HOttOpt, MovMethod.VAR, true);
highestHigh2 = HighestHighIndicator(Symbol1, SymbolPeriod1, MinorTrend3_REFHHV);
tott4 = TOTTIndicator(Symbol1, SymbolPeriod1, OHLCType.Close, MinorTrend4_TottPeriod, MinorTrend4_TottOpt, MinorTrend4_TottTwinOttCoef, MovMethod.VAR);
sott4 = SOTTIndicator(Symbol1, SymbolPeriod1, OHLCType.Close, MinorTrend4_SottPeriodK, MinorTrend4_SottPeriodSlowK, MinorTrend4_SottOttPeriod, MinorTrend4_SottOttOpt, MovMethod.VAR, MovMethod.VAR);
highestHigh3 = HighestHighIndicator(Symbol1, SymbolPeriod1, MinorTrend4_HHV / 2);
ott6 = OTTIndicator(highestHigh3, MinorTrend4_HOttPeriod, MinorTrend4_HOttOpt, MovMethod.VAR, true);
highestHigh4 = HighestHighIndicator(Symbol1, SymbolPeriod1, MinorTrend4_REFHHV);
ott7 = OTTIndicator(Symbol1, SymbolPeriod1, OHLCType.Close, FirsatciTrend_OttPeriod, FirsatciTrend_OttOpt, MovMethod.VAR, true);
SendOrderSequential(true, Side.Buy);
SendOrderSequentialForShort(true, Side.Sell);
WorkWithPermanentSignal(true);
SetLeverage(Symbol1, Kaldirac);
SetLeverageType(Symbol1, true);
SetTimerInterval(1);
}
public override void OnInitComplated()
{
}
public class OrderListTimestamp
{
public string ID;
public string Symbol;
public decimal Adet;
public decimal Fiyat;
public OrdType EmirTipi;
public OrderSide orderSide;
public string EmirYonu;
public DateTime TetiklenmeZamani;
public int Sayac;
public bool AktifMI;
}
Dictionary<string, OrderListTimestamp> timestampDict = new Dictionary<string, OrderListTimestamp>();
[Parameter(3)]
public int AyniEmirKacSeferGonderilsin;
[Parameter(10)]
public int KacSaniyeSonraTekrarGonderilsin;
string orderIDTimestamp = string.Empty;
public override void OnTimer()
{
var tutt = timestampDict.Where(x => x.Value.AktifMI == true && DateTime.Now >= x.Value.TetiklenmeZamani);
if (tutt.Count() >0)
{
foreach (var deger in tutt)
{
LastOrderSide.Obj = deger.Value.orderSide == OrderSide.Buy? Side.Sell:Side.Buy;
if (deger.Value.EmirTipi.Obj == OrdType.Limit)
{
orderIDTimestamp = SendLimitOrder(deger.Value.Symbol, deger.Value.Adet, deger.Value.orderSide, deger.Value.Fiyat);
Debug(deger.Value.EmirYonu + " emri tekrar gönderildi.");
}else if (deger.Value.EmirTipi.Obj == OrdType.Market)
{
orderIDTimestamp = SendMarketOrder(deger.Value.Symbol, deger.Value.Adet, deger.Value.orderSide);
Debug(deger.Value.EmirYonu + " emri tekrar gönderildi.");
}
deger.Value.ID = orderIDTimestamp;
deger.Value.AktifMI = false;
timestampDict[orderIDTimestamp] = deger.Value;
timestampDict.Remove(deger.Key);
}
}
}
public override void OnOrderUpdate(IOrder order)
{
if (order.OrdStatus.Obj == OrdStatus.Filled)
{
if (timestampDict.ContainsKey(order.CliOrdID))
{
timestampDict.Remove(order.CliOrdID);
Debug("Timestamp hatasına takılan emriniz gerçekleşti.");
}
}
if (order.OrdStatus.Obj == OrdStatus.Rejected)
{
if (!timestampDict.ContainsKey(order.CliOrdID))
{
OrderListTimestamp orderList = new OrderListTimestamp();
orderList.ID = order.CliOrdID;
orderList.Symbol = order.Symbol;
orderList.Adet = order.OrderQty;
orderList.Fiyat = order.Price;
orderList.EmirTipi = order.OrdType;
orderList.TetiklenmeZamani = DateTime.Now;
orderList.Sayac = 0;
orderList.AktifMI = false;
if (order.Side.Obj == Side.Buy)
{
orderList.orderSide = OrderSide.Buy;
orderList.EmirYonu = "Alış";
}else
{
orderList.orderSide = OrderSide.Sell;
orderList.EmirYonu = "Satış";
}
timestampDict[order.CliOrdID] = orderList;
}
if (order.Text.Contains("Timestamp"))
{
if (timestampDict.ContainsKey(order.CliOrdID))
{
if (timestampDict[order.CliOrdID].Sayac < AyniEmirKacSeferGonderilsin)
{
timestampDict[order.CliOrdID].TetiklenmeZamani = DateTime.Now.AddSeconds(KacSaniyeSonraTekrarGonderilsin);
timestampDict[order.CliOrdID].Sayac++;
timestampDict[order.CliOrdID].AktifMI = true;
Debug("Emir Timestamp hatasından dolayı iptal oldu tekrar gönderilecek");
}else
{
timestampDict.Remove(order.CliOrdID);
Debug("Aynı emir üst üste " + AyniEmirKacSeferGonderilsin + " defa gönderildi.");
}
}
}
}
}
}
}